package com.yanqu.road.server.manger.activity.turtle;

import com.yanqu.road.dao.impl.activity.turtle.TurtleConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.turtle.config.TurtleTemplate;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.logic.bussiness.activity.TurtleBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.turtle.TurtleModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-09-06
 * @Description : 乌龟对对碰Mgr
 */

public class TurtleMgr extends TempMgr {

    static TurtleConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("Reload turtle collision activity start!");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.Turtle.getValue()); // 加载开放的活动
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("No turtle collision activity in show time!");
            config = null;
        } else {
            ActivityInfo activityInfo = openActivityInfoList.get(0);
            int activityId = activityInfo.getActivityId();
            TurtleConfig turtleConfig = new TurtleConfig(activityInfo);
            Map<String, ActivityConfig> configMap = ActivityMgr.getActivityConfigMap(activityId); // 固定配置表t_s_activity_config

            if (configMap != null) {
                turtleConfig.setGoldId(configMap.get(eActivityConfigType.TURTLE_TOKEN_ID_1.getName()).getIntValue());
                turtleConfig.setSilverId(configMap.get(eActivityConfigType.TURTLE_TOKEN_ID_2.getName()).getIntValue());
                turtleConfig.setDollId(configMap.get(eActivityConfigType.TURTLE_TOKEN_ID_3.getName()).getIntValue());
                turtleConfig.setItemId(configMap.get(eActivityConfigType.TURTLE_ITEM_ID.getName()).getIntValue());
                turtleConfig.setBossImageId(configMap.get(eActivityConfigType.TURTLE_MALL_SHOW_ID.getName()).getIntValue());
                turtleConfig.setScoreId(configMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
                turtleConfig.setGuaranteeNum(configMap.get(eActivityConfigType.TURTLE_ISGOOD_NUM.getName()).getIntValue());
                turtleConfig.setStartNum(configMap.get(eActivityConfigType.TURTLE_START_NUM.getName()).getIntValue());
            }

            Map<Integer, TurtleTemplate> templateConfig = TurtleBusiness.getTemplateConfig(activityId);
            turtleConfig.setTemplateMap(templateConfig);
            for (Map.Entry<Integer, TurtleTemplate> entry : templateConfig.entrySet()) {
                int id = entry.getKey();
                TurtleTemplate template = entry.getValue();
                if (template.getIsGood() == 0) {
                    turtleConfig.getBadTemplateMap().put(id, template);
                } else {
                    turtleConfig.getGoodTemplateMap().put(id, template);
                }
            }
            turtleConfig.setModelMap(TurtleBusiness.getModelConfig(activityId));

            config = turtleConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(TurtleModule.class).activityReload();
        }

        getLogger().info("Reload turtle collision activity finish!");
    }

    public static TurtleConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo() {
        if (config == null) {
            return null;
        }
        return config.getActivityInfo();
    }
}
